package it.apiconsulting.sharem.controller;

import it.apiconsulting.sharem.controller.bean.UtenteBean;
import it.apiconsulting.sharem.dao.UtenteDao;
import it.apiconsulting.sharem.util.CipherUtils;

import java.sql.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterUtenteAction extends ActionSupport {

	private static final long serialVersionUID = 9149826260758390091L;
	private String username;
	private String password;
	private String nome;
	private String cognome;
	private String citta;
	private String pathVlc;
	
	private UtenteDao linkController;

	public RegisterUtenteAction() {
		linkController = new UtenteDao();
	}

	public String execute() {
		return SUCCESS;
	}
	
	public String save() {
		
		Date dataCreazione = new Date(new java.util.Date().getTime());
		try {
			final String encryptedPwd = CipherUtils.encrypt(password.trim());
			UtenteBean utente = new UtenteBean();
			utente.setPassword(encryptedPwd);
			utente.setUsername(username);
			utente.setCognome(cognome);
			utente.setNome(nome);
			utente.setPathVlc(pathVlc);
			utente.setDatanascita(dataCreazione);
			utente.setDatacreazione(dataCreazione);
			
			linkController.add(utente);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getCognome() {
		return cognome;
	}

	public void setCognome(String cognome) {
		this.cognome = cognome;
	}

	public String getCitta() {
		return citta;
	}

	public void setCitta(String citta) {
		this.citta = citta;
	}

	public String getPathVlc() {
		return pathVlc;
	}

	public void setPathVlc(String pathVlc) {
		this.pathVlc = pathVlc;
	}
}